package com.haizhi.crm.common.file;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 * Created by yangyijun on 2018/5/25.
 */
public final class ExcelRowBuilder {
    public static <T> T build(Class<T> clazz, String[] fieldNames, Row row, int fieldCnt) throws InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, java.lang.reflect.InvocationTargetException {
        T target = clazz.newInstance();
        for (int j = 0; j < fieldCnt; j++) {
            String fieldName = fieldNames[j];
            if (fieldName == null) {
                continue;
            }
            Cell cell = row.getCell(j);
            if (cell == null) {
                continue;
            }
            Class<?> fieldType = clazz.getDeclaredField(fieldName).getType();
            if (ReflectUtils.isDateType(clazz, fieldName)) {
                ReflectUtils.invokeSetter(target, fieldName, cell.getDateCellValue());
            } else if (Boolean.TYPE == fieldType) {
                ReflectUtils.invokeSetter(target, fieldName, cell.getBooleanCellValue());
            } else if (Byte.TYPE == fieldType || Short.TYPE == fieldType || Integer.TYPE == fieldType || Long.TYPE == fieldType) {
                String value = String.valueOf((long) cell.getNumericCellValue());
                ReflectUtils.invokeSetter(target, fieldName, ReflectUtils.parseType(value, fieldType));
            } else {
                ReflectUtils.invokeSetter(target, fieldName, cell.getStringCellValue());
            }
        }
        return target;
    }
}
